package com.ecommerce.dal.dao.impl;

import java.sql.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.ecommerce.dal.dao.IItemHistoryDAO;
import com.ecommerce.dal.entity.ItemHistory;
import com.ecommerce.dal.util.CustomHibernateDAOSupport;

@Repository("itemHistoryDAO")
public class ItemHistoryDAO extends CustomHibernateDAOSupport implements
		IItemHistoryDAO {

	@Override
	public void save(ItemHistory itemHistory) {
		getHibernateTemplate().save(itemHistory);

	}

	// @Override
	// public void update(ItemHistory itemHistory) {
	// getHibernateTemplate().update(itemHistory);
	//
	// }

	@Override
	public void delete(ItemHistory itemHistory) {
		getHibernateTemplate().delete(itemHistory);

	}

	@Override
	public List<ItemHistory> findByItemId(long itemId) {
		List list = getHibernateTemplate().find(
				"select itemHistory from ItemHistory itemHistory inner join itemHistory.item item where item.itemId=?",
				itemId);
		return ((list != null && list.size()>0) ?  list : null) ;
	}

	@Override
	public double retrieveTotalItemPrice(long itemId) {
		List list = getHibernateTemplate()
				.find("select itemHistory.itemTotalPrice from ItemHistory itemHistory inner join itemHistory.item item where item.itemId=? order by itemHistory.itemIncludeDate asc",
						itemId);
		return (Double)((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	public Date retrieveItemIncludeDate(long itemId) {
		List list = getHibernateTemplate()
				.find("select itemHistory.itemIncludeDate from ItemHistory itemHistory inner join itemHistory.item item where item.itemId=? order by itemHistory.itemIncludeDate desc",
						itemId);
		return (Date) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}
}
